package com.yiruantong.system.controller.tenant;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yiruantong.system.domain.tenant.SysClient;
import com.yiruantong.system.domain.tenant.bo.SysClientBo;
import com.yiruantong.system.domain.tenant.vo.SysClientVo;
import com.yiruantong.system.mapper.tenant.SysClientMapper;
import com.yiruantong.system.service.tenant.ISysClientService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import com.yiruantong.common.core.domain.R;
import com.yiruantong.common.core.validate.AddGroup;
import com.yiruantong.common.core.validate.EditGroup;
import com.yiruantong.common.excel.utils.ExcelUtil;
import com.yiruantong.common.idempotent.annotation.RepeatSubmit;
import com.yiruantong.common.log.annotation.Log;
import com.yiruantong.common.log.enums.BusinessType;
import com.yiruantong.common.mybatis.core.page.PageQuery;
import com.yiruantong.common.mybatis.core.page.TableDataInfo;
import com.yiruantong.common.web.core.AbstractController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户端管理
 *
 * @author YiRuanTong
 * @date 2023-06-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/tenant/client")
public class SysClientController extends AbstractController<SysClientMapper, SysClient, SysClientVo, SysClientBo> {

  private final ISysClientService sysClientService;

  /**
   * 查询客户端管理列表
   */
  @SaCheckPermission("system:client:list")
  @GetMapping("/list")
  public TableDataInfo<SysClientVo> list(SysClientBo bo, PageQuery pageQuery) {
    return sysClientService.queryPageList(bo, pageQuery);
  }

  /**
   * 导出客户端管理列表
   */
  @SaCheckPermission("system:client:export")
  @Log(title = "客户端管理", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(SysClientBo bo, HttpServletResponse response) {
    List<SysClientVo> list = sysClientService.queryList(bo);
    ExcelUtil.exportExcel(list, "客户端管理", SysClientVo.class, response);
  }

  /**
   * 获取客户端管理详细信息
   *
   * @param id 主键
   */
  @SaCheckPermission("system:client:query")
  @GetMapping("/{id}")
  public R<SysClientVo> getInfo(@NotNull(message = "主键不能为空")
                                @PathVariable Long id) {
    return R.ok(sysClientService.queryById(id));
  }

  /**
   * 新增客户端管理
   */
  @SaCheckPermission("system:client:add")
  @Log(title = "客户端管理", businessType = BusinessType.INSERT)
  @RepeatSubmit()
  @PostMapping()
  public R<Void> add(@Validated(AddGroup.class) @RequestBody SysClientBo bo) {
    return toAjax(sysClientService.insertByBo(bo));
  }

  /**
   * 修改客户端管理
   */
  @SaCheckPermission("system:client:edit")
  @Log(title = "客户端管理", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping()
  public R<Void> editData(@Validated(EditGroup.class) @RequestBody SysClientBo bo) {
    return toAjax(sysClientService.updateByBo(bo));
  }

  /**
   * 状态修改
   */
  @SaCheckPermission("system:client:edit")
  @Log(title = "客户端管理", businessType = BusinessType.UPDATE)
  @PutMapping("/changeStatus")
  public R<Void> changeStatus(@RequestBody SysClientBo bo) {
    return toAjax(sysClientService.updateUserStatus(bo.getId(), bo.getStatus()));
  }
}
